package com.allenkerr.controller;

import com.allenkerr.model.Person;
import com.allenkerr.service.PersonService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by 小柯(Allen Kerr) on 17.6.18 23:58.
 */
@RestController
@RequestMapping("/people")
public class PersonController {

    @Autowired
    private PersonService personService;

    /**
     * 查找所有Person
     */
    @GetMapping
    public List<Person> findAll() {
        return personService.findAll();
    }

    /**
     * 分页查询Person
     */
    @GetMapping("/list")
    public List<Person> findByPage(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                       @RequestParam(required = false, defaultValue = "2") Integer pageSize) {

        //使用 pageHelper 实现分页
        PageHelper.startPage(pageNum, pageSize);
        //执行查询
        List<Person> list = personService.findAll();
        return list;

//        PageInfo<Person> pageInfo = new PageInfo<>(list);
//        return pageInfo;
    }

    /**
     * 根据Id查找Person
     */
    @GetMapping("/id/{id}")
    public Person findById(@PathVariable Integer id) {
        return personService.findById(id);
    }

    /**
     * 根据名字查找Person
     */
    @GetMapping("/name")
    public Person findByName(String name) {
        return personService.findByName(name);
    }

    /**
     * 保存Person
     */
    @PostMapping("/save")
    public Person saveOne(Person person) {
        personService.saveOne(person);
        return person;//自增主键id的值已更新
    }

}
